package com.taxinfo.service.impl;

import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.taxinfo.dao.MainReportMapper;
import com.taxinfo.dao.StandardSubMapper;
import com.taxinfo.dao.SubMatchingMapper;
import com.taxinfo.pojo.dto.StandardSubDto;
import com.taxinfo.pojo.vo.StandardSub;
import com.taxinfo.pojo.vo.SubMatching;
import com.taxinfo.service.StandardSubService;
import com.taxinfo.utils.HttpClientUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author wanghy
 * @since 2025-01-20
 */
@Service("StandardSubServiceImpl")
public class StandardSubServiceImpl extends ServiceImpl<StandardSubMapper, StandardSub> implements StandardSubService {

    @Autowired
    private StandardSubMapper standardSubMapper;

    @Value("${url.subject}")
    private String requestedUrl;

    @Override
    public List<StandardSubDto> selectAllData(String subName,String sCondition) {
        return standardSubMapper.selectAllData(subName,sCondition);
    }

    @Override
    public void syncSubjectInfo(JSONArray jsonArray) {
        for(int i = 0; i < jsonArray.size(); i++) {
            JSONObject jsonObject = jsonArray.getJSONObject(i);
            if("1".equals(transformation(jsonObject.get("delFlg")))){
                continue;
            }
            StandardSub sourceBean = new StandardSub();
            sourceBean.setId(transformation(jsonObject.get("id")));
            sourceBean.setSubNo(transformation(jsonObject.get("subNo")));
            sourceBean.setPSubNo("");
            sourceBean.setSubName(transformation(jsonObject.get("subName")));
            sourceBean.setSubType(transformation(jsonObject.get("subType")));
            sourceBean.setDecType(transformation(jsonObject.get("decType")));
            sourceBean.setSubArea(transformation(jsonObject.get("subArea")));
            sourceBean.setMemo(transformation(jsonObject.get("memo")));
            sourceBean.setTaxDecNo(transformation(jsonObject.get("taxDecNo")));
            sourceBean.setTaxSubName(transformation(jsonObject.get("taxSubName")));
            sourceBean.setYearCol(transformation(jsonObject.get("yearCol")));
            sourceBean.setQuarCol(transformation(jsonObject.get("quarCol")));
            sourceBean.setMonthCol(transformation(jsonObject.get("monthCol")));
            int delNum = standardSubMapper.deleteById(transformation(jsonObject.get("id")));
            standardSubMapper.insert(sourceBean);
        }
    }

    @Override
    public List<StandardSubDto> selectMatchByKey(String prefix, String suffix,String category) {
        return standardSubMapper.selectMatchByKey(prefix,suffix,category);
    }

    @Override
    public String getSubjectInfo(HashMap<String, Object> request) throws Exception {
        return HttpClientUtil.get(requestedUrl,request);
    }

    private String transformation(Object obj){
        if(null==obj){
            return "";
        }else{
            return String.valueOf(obj);
        }
    }
}
